<?php
# Calendar v1.0
# Copyright (c) 2016 Victor Nabatov greenray.spb@gmail.com
# Calendar example

/** Alias for DIRECTORY_SEPARATOR */
define ('DS', DIRECTORY_SEPARATOR);
/** Alias for line feed */
define ('LF', PHP_EOL);
/** Root directory */
define ('ROOT',      '.'.DS);
/** Classes */
define ('CLASSES',   ROOT.'classes'.DS);
/** Translations */
define ('LANGUAGES', ROOT.'languages'.DS);
/** Example template */
define ('TEMPLATES', ROOT.'templates'.DS);

require_once CLASSES.'calendar.class.php';
require_once CLASSES.'template.class.php';

$lang = [];
#
# Choose language
#
include LANGUAGES.'english.php';
//include LANGUAGES.'russian.php';
//include LANGUAGES.'spanish.php';

$options['language'] = $lang['language'];
$options['locale']   = $lang['locale'];
$options['datetime'] = $lang['datetime'];
$options['timezone'] = '0';

$CALENDAR = new CALENDAR($options);

$today = time();

$yearCurrent  = $CALENDAR->formatTime('Y', $today);
$monthCurrent = $CALENDAR->formatTime('n', $today);
#
# Coosing of the year
#
if (!empty($_POST['year'])) {
    $year = $_POST['year'];
    if (($year >= 2010) && ($year <= $yearCurrent)) {
        $yearSelected = $year;
    }
} else  $yearSelected = $yearCurrent;
#
# Coosing of the month
#
if (!empty($_POST['month'])) {
    $month = $_POST['month'];
    if (($month >= 1) && ($month <= 12)) {
        $monthSelected = $month;
    }
} else  $monthSelected = $CALENDAR->formatTime('n', $today);

$CALENDAR->set($monthSelected, $yearSelected);

$events = [
    'event1' => 1467335965,
    'event2' => 1467422365,
    'event3' => 1467595165,
    'event4' => 1467597165,
    'event5' => 1467681565
];

foreach($events as $id => $time) {
    if (($CALENDAR->formatTime('Y', $time) === $yearSelected) && ($CALENDAR->formatTime('n', $time) === $monthSelected)) {
        $CALENDAR->event($CALENDAR->formatTime('d', $time), $id);
    }
}
#
# Highliting the current date
#
if (($yearSelected == $yearCurrent) && ($monthSelected == $monthCurrent)) {
    $CALENDAR->highlight($CALENDAR->formatTime('d', $today));
}

$options['extension'] = '.tpl.php';
$TEMPLATE = new TEMPLATE('calendar', $options);
$TEMPLATE->set('locale', $options['locale']);
$TEMPLATE->set($CALENDAR->create($yearCurrent, $yearSelected, $monthSelected));
echo $TEMPLATE->parse();
